package ping.pay.me.web;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

/**
 * Created by install on 14/12/17.
 */
public class HttpConnectionManager {

    public static String readParse(Context context,String urlPath) throws Exception{
//        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int len = 0;
        URL url = new URL(urlPath);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        StringBuffer sb = new StringBuffer();
        String str = null;
        InputStream inputStream = conn.getInputStream();
        while((len = inputStream.read(data))>0){
            str = new String(data,0,len);
            sb.append(str);
        }
        inputStream.close();
        Log.d("tag",sb.toString());
        return sb.toString();
    }

    public static Bitmap getImage(String urlPath) throws Exception{
        Bitmap bitmap = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        URL url = new URL(urlPath);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");

        if(conn.getResponseCode()==200){
            InputStream inputStream = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(inputStream);
            inputStream.close();
        }
        return bitmap;
    }

    public static HashMap<String,Object> analysis(String jsonStr) throws JSONException{
//        JSONArray jsonArray = null;
//        ArrayList<HashMap<String,Object>> result = new ArrayList<HashMap<String, Object>>();
//        jsonArray = new JSONArray(jsonStr);
//        Log.d("tag","analysis");
//        for(int i=0;i<jsonArray.length();i++){
//            JSONObject jsonObject = jsonArray.getJSONObject(i);
//            HashMap<String,Object> map = new HashMap<String, Object>();
//            map.put("img",jsonObject.getString("img"));
//            Log.d("tag",jsonObject.getString("img"));
//            result.add(map);
//        }
        HashMap<String,Object> result = new HashMap<String, Object>();
        JSONObject jsonObject = new JSONObject(jsonStr);
//        JSONArray jsonArray = jsonObject.getJSONArray("img");
//        for(int i=0;i<jsonArray.length();i++){
//            JSONObject img = jsonArray.getJSONObject(i);
//        }
//        Log.d("tag",jsonObject.getString("img"));
        result.put("img","http://"+jsonObject.getString("img"));

        return result;
    }

    public static boolean checkNetwork(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            return cm.getActiveNetworkInfo().isAvailable();
//            NetworkInfo[] info = cm.getAllNetworkInfo();
//            if (info != null) {
//                for (int i = 0; i < info.length; i++) {
//                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
//                        return true;
//                    }
//                }
//            }
        }
        return false;
    }
}
